package com.jlwl.houseservice.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.SpecialService;
import com.jlwl.houseservice.service.SpecialServiceService;
import com.jlwl.houseservice.vo.SpecialServiceDetailVO;
import com.jlwl.houseservice.vo.SpecialServiceQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员特色服务控制器
 */
@RestController
@RequestMapping("/admin/special-services")
@PreAuthorize("hasRole('ADMIN')")
public class AdminSpecialServiceController {

    @Autowired
    private SpecialServiceService specialServiceService;

    /**
     * 获取特色服务列表
     */
    @GetMapping
    public Result<Page<SpecialService>> list(SpecialServiceQueryVO queryVO) {
        Page<SpecialService> page = specialServiceService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取特色服务详情
     */
    @GetMapping("/{id}")
    public Result<SpecialServiceDetailVO> getDetail(@PathVariable Integer id) {
        SpecialServiceDetailVO detailVO = specialServiceService.getServiceDetail(id);
        if (detailVO == null) {
            return Result.error("特色服务不存在");
        }
        return Result.success(detailVO);
    }

    /**
     * 创建特色服务
     */
    @PostMapping
    public Result<Void> create(@RequestBody SpecialService specialService, @RequestParam(required = false) List<String> imageUrls) {
        boolean success = specialServiceService.createService(specialService, imageUrls);
        if (!success) {
            return Result.error("创建特色服务失败");
        }
        return Result.success();
    }

    /**
     * 更新特色服务
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody SpecialService specialService, @RequestParam(required = false) List<String> imageUrls) {
        specialService.setId(id);
        boolean success = specialServiceService.updateService(specialService, imageUrls);
        if (!success) {
            return Result.error("更新特色服务失败");
        }
        return Result.success();
    }

    /**
     * 删除特色服务
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean success = specialServiceService.deleteService(id);
        if (!success) {
            return Result.error("删除特色服务失败");
        }
        return Result.success();
    }

    /**
     * 更新特色服务状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        boolean success = specialServiceService.updateServiceStatus(id, status);
        if (!success) {
            return Result.error("更新特色服务状态失败");
        }
        return Result.success();
    }

    /**
     * 更新特色服务推荐状态
     */
    @PutMapping("/{id}/recommend")
    public Result<Void> updateRecommendStatus(@PathVariable Integer id, @RequestParam Integer isRecommended) {
        boolean success = specialServiceService.updateServiceRecommendStatus(id, isRecommended);
        if (!success) {
            return Result.error("更新特色服务推荐状态失败");
        }
        return Result.success();
    }
} 